import d4dset1
from multiprocessing import Process
from multiprocessing import Pool
from datetime import date
import pp
import sys

FILE_ANT_ID_ABIDJAN = "ant_in_abidjan.txt"


def init_ant_ids_in_abidjan():
    ant_ids = []
    with open(FILE_ANT_ID_ABIDJAN, 'r') as finput:
        for line in finput:
            ant_id = line.strip()
            ant_ids.append(ant_id)
    return ant_ids

def count_ants_calls(ant_ids, start_date, end_date):
    with open("tttemp{}_{}".format(ant_ids[0], ant_ids[-1]), 'w') as outputfile: 
        for ant_id in ant_ids:
            for cur_date in d4dset1.date_range(start_date, end_date):
                calls = d4dset1.sum_ant_related_all_calls(ant_id, cur_date)
                outputstr = "{}\t{}\t{}\n".format(ant_id, cur_date, calls)
                outputfile.write(outputstr)
            
if __name__ == '__main__':
    from time import clock
    from functools import partial
    start = clock() 
    start_date = date(2011,12,5)
    end_date = date(2012,4,22)
    ant_ids = init_ant_ids_in_abidjan()
   
    #split all the antennas to give to different processes
    split_num = 8
    id_num = len(ant_ids)
    split_length = len(ant_ids)/split_num
    sub_ant_ids = []
    for i in range(split_num): 
        sub_ant_ids.append(ant_ids[int(split_length*i):int(split_length*(i+1))])
   
    #following are 3 ways to use multiple cores

    #1. use paralell python packages
    #ppservers = ()
    #job_server = pp.Server(ppservers=ppservers)
    #jobs = [job_server.submit(count_ants_calls, (input, start_date, end_date, ), (),
        #("d4dset1", )) for input in sub_ant_ids]
    #results = [job() for job in jobs]
    #job_server.print_stats()

    #2. use proecesses in multiprocessing
    #ps = [Process(target=count_ants_calls, args=(ant_ids, start_date,
        #end_date,)) for ant_ids in sub_ant_ids]
    #for p in ps: p.start()
    #for p in ps: p.join()

    #3. use pool in multiprocessing
    pool = Pool(8)
    partial_count_ants_calls = partial(count_ants_calls, start_date=start_date,
            end_date=end_date)
    pool.map(partial_count_ants_calls, sub_ant_ids)

    end = clock()
    print "total {} seconds".format(end-start)
